<template>
  <div>
    <el-card class="box-card">
      <!-- 面包屑导航 -->
      <div>
        <el-breadcrumb separator="/">
          <el-breadcrumb-item>首页</el-breadcrumb-item>
          <el-breadcrumb-item>基本管理</el-breadcrumb-item>
          <el-breadcrumb-item active>仓库管理</el-breadcrumb-item>
        </el-breadcrumb>
      </div>
      <!-- 添加按钮 -->
      <el-button
        type="success"
        style="margin-top: 30px"
        @click="addDialogVisible = true"
        >添加仓库</el-button
      >
      <el-table
        :data="datalist"
        height="500"
        row-key="store_id"
        border
        style="width: 100%; margin-top: 30px; height: 100%"
      >
        <el-table-column prop="store_id" label="仓库编号" width="180">
        </el-table-column>
        <el-table-column prop="store_name" label="仓库名称" width="180">
        </el-table-column>
        <el-table-column prop="store_address" label="仓库地址">
        </el-table-column>
        <el-table-column prop="warning_up" label="预警上限"> </el-table-column>
        <el-table-column prop="warning_down" label="预警下限">
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              type="warning"
              @click="editDialog(scope.row)"
              icon="el-icon-edit"
            ></el-button>
            <el-button
              type="danger"
              @click="deleteDialog(scope.row)"
              icon="el-icon-delete"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 添加按钮 -->
    <el-dialog
      title="添加仓库"
      :visible.sync="addDialogVisible"
      width="30%"
      center
      :before-close="clearDialog"
    >
      <el-form :model="form" label-width="100px" class="demo-ruleForm">
        <el-form-item label="仓库名称" prop="pass">
          <el-input v-model="form.store_name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="仓库地址" prop="pass">
          <el-input v-model="form.store_address" autocomplete="off" clearable :maxlength="0">
              <i slot="suffix" class="el-icon-location-outline" @click="openmap"></i>
          </el-input>
        </el-form-item>
        <el-form-item label="预警上限" prop="pass">
          <el-input v-model="form.warning_up" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="预警下限" prop="pass">
          <el-input v-model="form.warning_down" autocomplete="off"></el-input>
        </el-form-item>
        <div v-if="map" class="mapbox">
        <v-map  class="baidumap" @childMarker="changemarker"></v-map>
        </div>

      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="clearDialog">取 消</el-button>
        <el-button type="primary" @click="addstoreclick">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 修改按钮 -->
    <el-dialog
      title="修改仓库"
      :visible.sync="editDialogVisible"
      width="30%"
      center
      :before-close="clearDialog"
    >
      <el-form :model="form" label-width="100px" class="demo-ruleForm">
        <el-form-item label="仓库名称" prop="pass">
          <el-input v-model="form.store_name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="仓库地址" prop="pass">
          <el-input v-model="form.store_address" autocomplete="off" clearable :maxlength="0">
            <i slot="suffix" class="el-icon-location-outline" @click="openmap"></i>
          </el-input>
        </el-form-item>
        <el-form-item label="预警上限" prop="pass">
          <el-input v-model="form.warning_up" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="预警下限" prop="pass">
          <el-input v-model="form.warning_down" autocomplete="off"></el-input>
        </el-form-item>
        <div v-if="map" class="mapbox">
        <v-map  class="baidumap" @childMarker="changemarker"></v-map>
        </div>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="clearDialog">取 消</el-button>
        <el-button type="primary" @click="editstoreclick">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { addstore, editstore, delstore } from "../../http/api";
import { mapActions, mapGetters } from "vuex";
import vMap from "../../components/Baidu/baidu.vue";
import BaiduMap from "vue-baidu-map/components/map/Map.vue";
export default {
  data() {
    return {
      // datalist: [],
      //   添加提示框
      map: false,
      addDialogVisible: false,
      editDialogVisible: false,
      form: {
        store_id: "",
        store_name: "",
        store_address: "",
        warning_up: "",
        warning_down: "",
        lat:null,
        lng:null,
        is_del: 0,
      },
      search: {
        left: "",
        right: "",
      },
    };
  },
  created() {
    this.getstorelist();
  },
  components: {
    vMap,
    BaiduMap,
  },
  methods: {
    ...mapActions({
      getstorelist: "store/getstorelistActions",
    }),
    //添加仓库
    addstoreclick() {
      let that = this;
      addstore(this.form).then((res) => {
        if (res.status == 201) {
          //   that.datalist = res.data;
          that.$message.success(res.message);
          that.getstorelist();
        } else {
          console.log(res);
          that.$message.error(res.message);
        }
      });

      this.clearDialog();
    },
    // 点击修改按钮
    editDialog(data) {
      this.editDialogVisible = true;
      this.form = {
        store_id: data.store_id,
        store_name: data.store_name,
        store_address: data.store_address,
        warning_up: data.warning_up,
        warning_down: data.warning_down,
        is_del: data.is_del,
        lat:data.lat,
        lng:data.lng,
      };
    },
    // 确认修改
    editstoreclick() {
      let that = this;
      editstore(this.form).then((res) => {
        if (res.status == 201) {
          //   that.datalist = res.data;
          that.$message.success(res.message);
          that.getstorelist();
        } else {
          that.$message.error(res.message);
        }
      });

      this.clearDialog();
    },
    // 删除数据
    deleteDialog(data) {
      let that = this;
      this.$confirm("此操作将永久删除该仓库, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          delstore(data).then((res) => {
            if (res.status == 201) {
              //   that.datalist = res.data;
              that.$message.success(res.message);
              that.getstorelist();
            } else {
              console.log(res);
              that.$message.error(res.message);
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    clearDialog() {
      this.editDialogVisible = false;
      this.addDialogVisible = false;
      this.form = {
        store_id: "",
        store_name: "",
        store_address: "",
        warning_up: "",
        warning_down: "",
        lat:null,
        lng:null,
        is_del: 0,
      };
      // this.map = false;
      this.closemap()
    },
    //打开地图
    openmap() {
      this.map = !this.map;
    },
    closemap(){
      this.map = false;
    },
    //获取地址坐标
    changemarker(data){
      console.log(data,'我是父组件');
      this.form.store_address=data.address+'('+data.title+')'
      this.form.lat=data.lat,
      this.form.lng=data.lng,
      this.closemap()
    }
  },
  computed: {
    ...mapGetters({
      datalist: "store/datalist",
    }),
  },
};
</script>

<style lang="less" scoped>
.bm-view {
  width: 100%;
  height: 100%;
}
.el-form {
  position: relative;
}
.baidumap {

}
.mapbox{
  width: 550px;
  height: 500px;
  padding: 10px;
  position: absolute;
  top: -200px;
  right: -580px;
  // margin-left: 500px;
  background-color: #fff;
  border-radius: 10px;
}
</style>